import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class Tool {
    private static final Scanner input = new Scanner(System.in);

    public static void resetPassword(User user) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入原密码：");
        if (input.next().equals(user.getUserPassword())) {
            System.out.println("请输入新密码：(需同时包含字母和数字，不含有其他字符，长度区间[6,15])");
            boolean direct = false;
            String newPassword = "";
            while (!direct) {
                newPassword = input.next();
                direct = checkPassword(newPassword);
                if (!direct)
                    System.out.println("密码格式错误，请重新输入！");
            }
            user.setUserPassword(newPassword); //改密
            System.out.println("\n-> 修改密码成功\n");
            log("修改密码", user);
        }
        else
            System.out.println("密码输入错误！返回上一级...\n");
    }

    public static void resetLowLevelPassword(Administrator operator) {
        Scanner input = new Scanner(System.in);
        boolean loop;
        System.out.println("请选择用 ID 查找或是用 用户名 查找欲重置密码的下级用户：\n1.ID  2.用户名");
        String findUser;
        do {
            switch (input.next()) {
                case "1":
                    System.out.println("请输入欲重置密码用户的ID：");
                    findUser = input.next();
                    for (User user : CinemaSystem.userList) {
                        if (user.getUserID().equals(findUser)) {
                            if (!user.getUserType().equals(LoginKey.ADMIN)) {
                                user.setUserPassword("123456");
                                System.out.println("\n给" + user.getUserName() +"("+user.getUserType().toString()+")"+
                                        "重置密码为初始密码(123456)成功!\n");
                                log("给" + user.getUserName() +"("+user.getUserType().toString()+")"+ "重置密码", operator);
                            } else
                                System.out.println("您无权进行操作，不可重置密码！返回上一级...\n");
                            return;
                        }
                    }
                    loop = false;
                    break;
                case "2":
                    System.out.println("请输入欲重置密码用户的用户名：");
                    findUser = input.next();
                    for (User user : CinemaSystem.userList) {
                        if (user.getUserName().equals(findUser)) {
                            if (!user.getUserType().equals(LoginKey.ADMIN)) {
                                user.setUserPassword("123456");
                                System.out.println("\n给" + user.getUserName() +"("+user.getUserType().toString()+")"+
                                        "重置密码为初始密码(123456)成功!\n");
                                log("给" + user.getUserName() +"("+user.getUserType().toString()+")"+ "重置密码", operator);
                            } else
                                System.out.println("您无权进行操作，不可重置密码！返回上一级...\n");
                            return;
                        }
                    }
                    loop = false;
                    break;
                default:
                    System.out.println("输入错误，请重新选择： 1.ID  2.用户名");
                    loop = true;
            }
        } while (loop);
        System.out.println("未找到该用户！返回上一级...\n");
    }

    public static void resetLowLevelPassword(Manager operator) { //重载
        Scanner input = new Scanner(System.in);
        boolean loop;
        System.out.println("请选择用 ID 查找或是用 用户名 查找欲重置密码的职员：\n1.ID  2.用户名");
        String findUser;
        do {
            switch (input.next()) {
                case "1":
                    System.out.println("请输入欲重置密码用户的ID：");
                    findUser = input.next();
                    for (User user : CinemaSystem.userList) {
                        if (user.getUserID().equals(findUser)) {
                            if (!(user.getUserType().equals(LoginKey.ADMIN) || user.getUserType().equals(LoginKey.MANAGER))) {
                                user.setUserPassword("123456");
                                System.out.println("\n给" + user.getUserName() +"("+user.getUserType().toString()+")"+
                                        "重置密码为初始密码(123456)成功!\n");
                                log("给" + user.getUserName() +"("+user.getUserType().toString()+")"+ "重置密码", operator);
                            } else
                                System.out.println("您无权进行操作，不可重置密码！返回上一级...\n");
                            return;
                        }
                    }
                    loop = false;
                    break;
                case "2":
                    System.out.println("请输入欲重置密码用户的用户名：");
                    findUser = input.next();
                    for (User user : CinemaSystem.userList) {
                        if (user.getUserName().equals(findUser)) {
                            if (!(user.getUserType().equals(LoginKey.ADMIN) || user.getUserType().equals(LoginKey.MANAGER))) {
                                user.setUserPassword("123456");
                                System.out.println("\n给" + user.getUserName() +"("+user.getUserType().toString()+")"+
                                        "重置密码为初始密码(123456)成功!\n");
                                log("给" + user.getUserName() +"("+user.getUserType().toString()+")"+ "重置密码", operator);
                            } else
                                System.out.println("您无权进行操作，不可重置密码！返回上一级...\n");
                            return;
                        }
                    }
                    loop = false;
                    break;
                default:
                    System.out.println("输入错误，请重新选择： 1.ID  2.用户名");
                    loop = true;
            }
        } while (loop);
        System.out.println("未找到该用户！返回上一级...\n");
    }

    public static String spaceStack(int n) { //叠加空格
        return " ".repeat(Math.max(0, n));
    }

    public static boolean checkPassword(String password) { //密码需同时包含字母和数字，不含有其他字符，长度区间[6,15]
        if (password.length() > 15 || password.length() < 6)
            return false;
        int letterCounts = 0, numberCounts = 0;
        for (int c = 0; c<password.length(); c++) {
            if ((password.charAt(c) >= 'a' && password.charAt(c) <= 'z') ||
                    (password.charAt(c) >= 'A' && password.charAt(c) <= 'Z'))      letterCounts++;
            if (password.charAt(c) >= '0' && password.charAt(c) <= '9')        numberCounts++;
        }                                                     //如果不相等说明含有其他字符
        return letterCounts!= 0 && numberCounts != 0 && password.length() == letterCounts+numberCounts;
    }

    public static LoginKey checkIdentify() { //身份验证
        String account, password;
        System.out.println("请输入账号：");
        account = input.next();
        for (User user : CinemaSystem.userList) {
            if (account.equals(user.getUserID())) { //寻找到对应账号
                //新账号或被重置密码的账号
                if (user.getUserPassword().equals("123456")) {
                    System.out.println("您的密码为初始密码，需要更改密码，请输入新密码(需同时包含字母和数字，不含有其他字符，长度区间[6,15]):");
                    boolean direct;
                    while (true) {
                        String newPassword = input.next();
                        direct = checkPassword(newPassword);
                        if (direct) {
                            System.out.println("更改密码成功！请返回界面重新登录。 (按任意字符返回)");
                            user.setUserPassword(newPassword); //更改密码
                            input.next();
                            return LoginKey.INVALID;
                        }
                        else
                            System.out.println("输入不合规范，请重新输入：");
                    }
                }
                //账密信息正常用户
                else {
                    while (true) {
                        System.out.println("请输入密码：");
                        password = input.next();
                        if (password.equals(user.getUserPassword())) {
                            System.out.println("验证成功！欢迎使用本系统，" + user.getUserName() + "(" +
                                    user.getUserType().toString() + ")");
                            CinemaSystem.currentUser = user;
                            return user.getUserType();
                        }
                        else { //密码验证不成功
                            System.out.println("密码输入错误，请重新输入密码或联系管理员重置密码\n请选择： 1.重新输入  0.返回登陆界面)");
                            boolean loop = true;
                            while (loop) {
                                switch (input.next()) {
                                    case "1":
                                        loop = false;
                                        break;
                                    case "0":
                                        return LoginKey.INVALID; //返回登录界面
                                    default:
                                        System.out.println("输入错误，请重新选择： (1.重新输入  0.返回登陆界面)");
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println("系统内没有该账号，返回登录界面...");
        return LoginKey.INVALID;
    }

    public static void log(String description, User operator) { //操作描述，操作人
        //将日志写入currentLog
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String[] tempLog = new String[] { currentTime, description, operator.getUserName() };
        CinemaSystem.currentLog.add(tempLog);
    }
}
